﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.Utils;
using Markdig;
using System.IO;
using EasyClassUI.entity;
using EasyClassUI.WebRequest;
using System.Threading.Tasks;
using EasyClassUI.forms.blog;

namespace EasyClassUI
{
    public partial class WriteBlog : Form
    {
        private Boolean mouseDown = false, sizeChanged = false, needSave = false;
        private String fileSavePath = "";
        public WriteBlog()
        {
            InitializeComponent();
            errMsg.Visible = false;
            groupsBox.Items.Clear();
            foreach(StudyGroup group in StudyGroup.GroupIJoined) {
                groupsBox.Items.Add(group.StudyGroupName);
            }
        }

        private void panelHandle_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void panelHandle_MouseLeave(object sender, EventArgs e)
        {
            mouseDown = false;
        }

        private void panelHandle_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void textBoxFrom_TextChanged(object sender, EventArgs e)
        {
            if (!needSave)
            {
                this.Text = "● " + this.Text;
            }
            needSave = true;
            String headHtml = "<!doctype html><html><head><meta http-equiv=\"content-type\" Content=\"text/html;charset=utf-8\"/><style>body{font-family:\"Microsoft Yahei\";}</style></head><body>";
            String content = Markdown.ToHtml(textBoxFrom.Text.ToString());
            String endHtml = "</body></html>";
            webBrowserTo.DocumentText = headHtml + content + endHtml;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (blogNameTB.Text=="" || blogCategoryComboBox.Text == "") {
                errMsg.Visible = true;
                errMsg.Text = "Please enter your post infomations.";
                return;
            }

            if (!Directory.Exists(@".\MyPosts\")) {
                Directory.CreateDirectory(@".\MyPosts\");
            }

            string time = DateTime.Now.ToString("yyyy-M-dd--HH-mm-ss");
            string mdfile = $@".\MyPosts\{ time + ".md"}";  //文件名
            FileManagement.WriteToFile(mdfile, textBoxFrom.Text.ToString());
            fileSavePath = mdfile;
            needSave = false;
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;//等于true表示可以选择多个文件
            dlg.Title = "Please select files to upload.";
            if (dlg.ShowDialog() == DialogResult.OK) {
                List<string> fileToUpload = dlg.FileNames.ToList();
                PostService postService = new PostService();
                Post newPost = new Post() { Title = blogNameTB.Text, Category = blogCategoryComboBox.Text, ContentName = time + ".md", StudyGroupID = StudyGroup.GroupIJoined[groupsBox.SelectedIndex].StudyGroupID };
                string result = postService.UploadPost(fileToUpload, mdfile, newPost, User.CurrentUser.Token);
                if (result == "200") {
                    errMsg.Visible = true;
                    errMsg.Text = "Upload successed!";
                }

                else {
                    errMsg.Visible = true;
                    errMsg.Text = "Something went wrong, please check your title and networks.";
                }
            }
        }

        private void WriteBlog_FormClosing(object sender, FormClosingEventArgs e) {
            blogUC.blogView.initializeCell();
        }

        private void WriteBlog_Load(object sender, EventArgs e)
        {
            compareControlSize();
        }
        private void compareControlSize()
        {
            if (!sizeChanged)
            {
                textBoxFrom.Width = (this.Width / 2) - 2;
                webBrowserTo.Left = (textBoxFrom.Width) + 4;
                webBrowserTo.Width = (this.Width / 2) - 2;
                panelHandle.Width = 4;
                panelHandle.Left = textBoxFrom.Width;
            }
            else
            {
                panelHandle.Left = textBoxFrom.Width;
                webBrowserTo.Left = panelHandle.Left + 4;
                webBrowserTo.Width = this.Width - webBrowserTo.Left;
            }
        }
    }
}
